package org.tio.sitexxx.web.server.controller.base;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tio.http.common.HttpRequest;
import org.tio.http.server.annotation.RequestPath;
import org.tio.sitexxx.service.model.main.IpInfo;
import org.tio.sitexxx.service.model.main.User;
import org.tio.sitexxx.service.service.base.RegisterService;
import org.tio.sitexxx.service.service.base.UserService;
import org.tio.sitexxx.service.vo.Const;
import org.tio.sitexxx.service.vo.RequestExt;
import org.tio.sitexxx.web.server.utils.WebUtils;
import org.tio.utils.resp.Resp;

import com.jfinal.plugin.activerecord.Db;

/**
 *
 * 
 */
@RequestPath(value = "/register")
public class RegisterController {
	@SuppressWarnings("unused")
	private static Logger log = LoggerFactory.getLogger(RegisterController.class);

	private static final RegisterService registerService = RegisterService.me;

	/**
	 * 激活，发送给用户注册邮箱中的带有 authCode 的激活链接指向该 action
	 * @throws Exception 
	 */
	@RequestPath(value = "/activate")
	public Resp activate(String authCode, HttpRequest request) throws Exception {
		Resp resp = registerService.activate(authCode);
		return resp;
	}

	/**
	 * 邮箱注册
	 * @param user
	 * @param request
	 * @return
	 * @author tanyaowu
	 */
	@RequestPath(value = "/emailRegister")
	public Resp emailRegister(User user, HttpRequest request) throws Exception {
		RequestExt requestExt = WebUtils.getRequestExt(request);
		Resp resp = registerService.emailRegister(user, request.getClientIp(), request.getHttpSession().getId(), requestExt);
		return resp;
	}

	/**
	 * 
	 * @param user
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestPath(value = "/xx")
	public Resp xx(User user, HttpRequest request) throws Exception {
		User currUser = WebUtils.currUser(request);
		if (currUser == null || !UserService.isSupper(currUser)) {
			return Resp.fail("你没资格操作");
		}

		completeUser(user, request);

		String sql = "select min(id) from user";

		Integer uid = Db.use(Const.Db.TIO_SITE_MAIN).queryInt(sql);
		user.setId(uid - 1);

		return registerService.innerEmailRegister(user, true);
	}

	@RequestPath(value = "/bxx")
	public Resp bxx(Long loginnameStartIndex, String pwd, String nicks, HttpRequest request) throws Exception {
		User currUser = WebUtils.currUser(request);
		if (currUser == null || !UserService.isSupper(currUser)) {
			return Resp.fail("你没资格操作");
		}

		String sql = "select min(id) from user";

		Integer uid = Db.use(Const.Db.TIO_SITE_MAIN).queryInt(sql);

		Resp resp = null;
		String[] nickarray = nicks.split(",");
		for (String nick : nickarray) {
			User user = new User();
			user.setNick(nick);
			user.setPwd(pwd);
			user.setLoginname(loginnameStartIndex++ + "@136.com");
			user.setId(--uid);
			completeUser(user, request);
			resp = registerService.innerEmailRegister(user, true);
		}

		return resp;
	}

	/**
	 * 默认注册路径是走的邮箱注册
	 * @param user
	 * @param regType 注册类型：1、邮箱，2、短信
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestPath(value = "/{regType}", forward = "/register/submit")
	public Resp register(User user, Byte regType, HttpRequest request) throws Exception {
		if (regType == 1 || regType == 2) {
			completeUser(user, request);

			if (regType == 1) {
				return emailRegister(user, request);
			}
		}

		return Resp.fail("参数不对");
	}

	/**
	 * 用邮箱找回密码（第一步：发地址到邮箱）
	 * @param loginname
	 * @param request
	 * @return
	 * @throws Exception
	 * @author tanyaowu
	 */
	@RequestPath(value = "/retrievePwd")
	public Resp retrievePwd(String loginname, HttpRequest request) throws Exception {
		RequestExt requestExt = WebUtils.getRequestExt(request);
		return registerService.retrievePwd(loginname, request.getClientIp(), request.getHttpSession().getId(), requestExt);
	}

	/**
	 * 设置新密码（第二步：设置新密码）
	 * @param authCode
	 * @param request 
	 * @return
	 * @throws Exception
	 */
	@RequestPath(value = "/setNewPwd")
	public Resp setNewPwd(String authCode, String newpwd, HttpRequest request) throws Exception {
		return registerService.setNewPwd(authCode, newpwd);
	}

	private void completeUser(User user, HttpRequest request) {
		String ip = request.getClientIp();
		IpInfo ipObj = IpInfo.from(ip);
		user.setIpInfo(ipObj);
	}

}
